package com.sxkiler.demo.biz.impl;

import com.sxkiler.demo.biz.dao.ATAccountDao;
import com.sxkiler.demo.biz.service.ATAccountService;
import com.sxkiler.demo.rpc.model.RpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@RpcService(serviceName = "ATAccountService")
public class ATAccountServiceImpl implements ATAccountService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ATAccountDao accountDao;

    @Override
    public Boolean reduceBalance(Long userId, Integer amount) throws Exception {
//        logger.info("[reduceBalance] 当前 XID: {}", RootContext.getXID());

        // 检查余额
        checkBalance(userId, amount);

        logger.info("[reduceBalance] 开始扣减用户 {} 余额", userId);
        // 扣除余额
        int updateCount = accountDao.reduceBalance(userId, amount);
        // 扣除成功
        if (updateCount == 0) {
            logger.warn("[reduceBalance] 扣除用户 {} 余额失败", userId);
            throw new Exception("余额不足");
        }
        logger.info("[reduceBalance] 扣除用户 {} 余额成功", userId);
        return true;
    }

    private void checkBalance(Long userId, Integer price) throws Exception {
        logger.info("[checkBalance] 检查用户 {} 余额", userId);
        Integer balance = accountDao.getBalance(userId);
        if (balance < price) {
            logger.warn("[checkBalance] 用户 {} 余额不足，当前余额:{}", userId, balance);
            throw new Exception("余额不足");
        }
    }

}
